package org.vectrics.project.tags;

import java.util.Iterator;
import java.util.List;
import org.apache.ecs.html.A;
import org.vectrics.common.html.BorderTable;
import org.vectrics.common.html.Cell;
import org.vectrics.common.html.Form;
import org.vectrics.common.html.Row;
import org.vectrics.common.html.Table;
import org.vectrics.common.html.TextHelper;
import org.vectrics.common.pagelist.PageList;
import org.vectrics.common.util.DateUtil;
import org.vectrics.project.task.Task;
import org.vectrics.project.task.TaskResource;
import org.vectrics.project.task.TaskStatusType;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.struts.PageListTag;

public class TaskPageListTag extends PageListTag {
	static final long serialVersionUID = 42356235;
	private String personId = null;
	private boolean showResources = true;
	
	public TaskPageListTag() {
		super();
	}
	protected String getDescription() {
		return(null);
	}

    public String getPageListLink() {
        return(getContextPath() + "/admin/task/mytasks.do");
    }
	

	protected void showList(String description, Cell mainCell,
			List tasks, Form form) {
		BorderTable table = new BorderTable();
		table.setWidth(this.getWidth());
		mainCell.addElement(table);
		makeHeaderRow(table);
		
		Iterator iter = tasks.iterator();
		while (iter.hasNext()) {
			Task task = (Task)iter.next();
			Row row = table.addRow();
			Cell cell = null;
			cell = row.addCell();
			cell.addElement(TextHelper.getFont("subFont", this.getPhrase(task.getDescription())));

			cell = row.addCell();
			String dueDateStr = "";
			if (task.getDueDate() != null) {
				dueDateStr = CoreServiceLocator.getDateFormat().getDateFormatRoundToMinute(this.getLocale()).format(task.getDueDate());
			}
			cell.addElement(TextHelper.getFont("subFont", dueDateStr));

			cell = row.addCell();
			cell.addElement(TextHelper.getFont("subFont", getPhrase(task.getCurrentStatus().getTaskStatusType().getPhraseCode())));
			
			if (this.showResources) {
				cell = row.addCell();
				Table resourceTable = new Table();
				cell.addElement(resourceTable);
				Iterator resIter = task.getTaskResources().iterator();
				while (resIter.hasNext()) {
					TaskResource taskResource = (TaskResource)resIter.next();
					Row resRow = resourceTable.addRow();
					Cell resNameCell = resRow.addCell();
					resNameCell.addElement(TextHelper.getFont("subFont", taskResource.getPerson().getName()));
				}
			}

			cell = row.addCell();
			A viewLink = new A();
			viewLink.setHref(this.getRequest().getContextPath() + "/admin/task/view.do?taskId="
					+ task.getId());
			viewLink.setClass("subLink");
			viewLink.addElement(this.getPhrase("phrase.view"));
			cell.addElement(viewLink);
			
		}
	}

	private void makeHeaderRow(Table table) {
		Row row = table.addRow();
        row.setDefaultBgImage(this.getContextPath() + "/site-graphics/tbl_hdr.gif");

        Cell contentHdrCell = row.addCell();
        contentHdrCell.addElement(TextHelper.getFont("tableHdrFont", this.getPhrase("prompt.task.description")));

        contentHdrCell = row.addCell();
        contentHdrCell.addElement(TextHelper.getFont("tableHdrFont", this.getPhrase("prompt.task.date.due")));

        contentHdrCell = row.addCell();
        contentHdrCell.addElement(TextHelper.getFont("tableHdrFont", this.getPhrase("prompt.task.status")));
		
        if (this.showResources) {
            contentHdrCell = row.addCell();
            contentHdrCell.addElement(TextHelper.getFont("tableHdrFont", this.getPhrase("prompt.task.resources")));
        }

        // For view link
        contentHdrCell = row.addCell();
	}
	
	protected PageList createPageList() {
		TaskPageList pageList = new TaskPageList();
		pageList.addStatusType(TaskStatusType.TASK_STATUS_CREATED_CODE);
		pageList.addStatusType(TaskStatusType.TASK_STATUS_ASSIGNED_CODE);
		pageList.addStatusType(TaskStatusType.TASK_STATUS_PENDING_CODE);
		return(pageList);
	}

	/**
	 * @return Returns the personId.
	 */
	public String getPersonId() {
		return personId;
	}

	/**
	 * @param personId The personId to set.
	 */
	public void setPersonId(String personId) {
		this.personId = personId;
	}
	
	
}
